//
//  OrderSucessViewController.m
//  XDLookPic
//
//  Created by wanglong8889@126.com on 14-5-25.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "OrderSucessViewController.h"
#import "ShouYinTaiViewController.h"
//#import "orderListViewController.h"
#import "patSingleton.h"

@interface OrderSucessViewController ()

@end

@implementation OrderSucessViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = @"提交成功";
//    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"pd_25_bai"] forState:UIControlStateNormal];
    [self.leftBtn removeFromSuperview];
    self.contentView.backgroundColor = RGBA(247, 244, 239, 1.0);
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [rightButton setTintColor:[UIColor whiteColor]];
    [rightButton setFrame:CGRectMake(520/2.0f, 13+aHeight, 60, 25)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [rightButton addTarget:self action:@selector(completeShop) forControlEvents:UIControlEventTouchUpInside];
    if (IOS7) {
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS7"];
    }else{
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS6"];
    }
    self.titleLabel.textColor = [UIColor whiteColor];
    
    [self.navigationBarView addSubview:rightButton];
    
    [self addSubviews];
    
    // Do any additional setup after loading the view.
}

- (void)addSubviews
{
    
    [[XDTools addAlabelForAView:self.contentView withText:@"订单提交成功！" frame:CGRectMake(10.0, 32.0, 300.0, 30) font:[UIFont systemFontOfSize:30.0] textColor:RGBA(209, 67, 45, 1.0)] setTextAlignment:NSTextAlignmentCenter];
    UIView * bottomview = [[UIView alloc]initWithFrame:CGRectMake(8.0, 80.0, 304.0, 120.0)];
    bottomview.layer.borderColor = UIColorFromRGB(0X9d8a8a).CGColor;
    bottomview.layer.borderWidth = 1.0;
    bottomview.layer.cornerRadius = 10.0;
    bottomview.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bottomview];
    
    [XDTools addLineToView:bottomview frame:CGRectMake(0.0, 39.5, 304.0, 1.0) color:UIColorFromRGB(0X9d8a8a)];
    [XDTools addLineToView:bottomview frame:CGRectMake(0.0, 79.5, 304.0, 1.0) color:UIColorFromRGB(0X9d8a8a)];
    
    idLanbel = [XDTools addAlabelForAView:bottomview withText:[NSString stringWithFormat:@"订单号：%@",self.orderId] frame:CGRectMake(15.0, 12.0, 284.0, 16.0) font:[UIFont systemFontOfSize:15.0] textColor:UIColorFromRGB(0X333333)];
    
    paynumberLabel = [XDTools addAlabelForAView:bottomview withText:[NSString stringWithFormat:@"订单金额：￥%@",self.totalMo] frame:CGRectMake(15.0, 52.0, 284.0, 16.0) font:[UIFont systemFontOfSize:15.0] textColor:UIColorFromRGB(0X333333)];
    
    paynumberLabel.attributedText = [XDTools getAcolorfulStringWithText1:@"订单金额:  " Color1:UIColorFromRGB(0X333333) Font1:[UIFont systemFontOfSize:15.0] Text2:self.totalMo Color2:UIColorFromRGB(0Xdc4e2a) Font2:[UIFont systemFontOfSize:15.0] AllText:paynumberLabel.text];
    
    [XDTools addAlabelForAView:bottomview withText:@"支付方式:  在线支付" frame:CGRectMake(15.0, 92.0, 284.0, 16.0) font:[UIFont systemFontOfSize:15.0] textColor:UIColorFromRGB(0X333333)];
    
    [XDTools addAlabelForAView:self.contentView withText:@"请在72小时之内完成支付，过时订单将会被自动取消。" frame:CGRectMake(20, 211.0, 304.0, 20.0) font:[UIFont systemFontOfSize:12.0] textColor:UIColorFromRGB(0X9d948a)];

    UIButton * onlinePayButton = [XDTools getAButtonWithFrame:CGRectMake(60.0, 250.0, 200.0, 40.0) nomalTitle:@"在线支付" hlTitle:@"在线支付" titleColor:nil bgColor:nil nbgImage:@"write_tijiao" hbgImage:@"write_tijiao" action:@selector(payOnlineAction) target:self buttonTpye:UIButtonTypeCustom];
    [onlinePayButton setBackgroundImage:[[UIImage imageNamed:@"gotopay"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 5, 5, 10)] forState:UIControlStateNormal];
     [onlinePayButton setBackgroundImage:[[UIImage imageNamed:@"gotopay"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 5, 5, 10)] forState:UIControlStateHighlighted];
    
    
    onlinePayButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [self.contentView addSubview:onlinePayButton];
    
}

- (void)payOnlineAction{
    ShouYinTaiViewController *taiVC = [[ShouYinTaiViewController alloc] init];
    taiVC.orderId = self.orderId;
    taiVC.totalMo = self.totalMo;
    taiVC.goodsName = self.goodName;
    [patSingleton sharePatSingleton].shouVC = taiVC;
    [self.navigationController pushViewController:taiVC animated:YES];
}


//回到首页
-(void)completeShop{
//    orderListViewController * ovc = [[orderListViewController alloc] init];
//    ovc.type = @"0";
//    [self.navigationController pushViewController:ovc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//获取订单

- (void)commitDingDanDatas{
    
    
    NSString * uid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MYSELF_INFO"] objectForKey:@"uid"];
    
    if ([XDTools NetworkReachable])
    {
        NSDictionary * dic = @{@"orderID":self.orderId,
                               @"userID":uid,
                               };
        
        
        ASIHTTPRequest *request = [XDTools postECShopRequestWithDict:dic API:API_ORDERDETAIL sign:@"1"];
        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if ([[tempDic objectForKey:@"status"] intValue] == 1)
            {
                NSDictionary *dic = [[tempDic objectForKey:@"data"] objectForKey:@"order_info"];
                idLanbel.text = [NSString stringWithFormat:@"订单号：%@",dic[@"order_sn"]];
                paynumberLabel.text = [NSString stringWithFormat:@"订单金额：￥%@",dic[@"order_amount"]];
                
            }else{
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
            }
            
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (mrequest.error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        [XDTools showProgress:self.view];
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
